﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using HHG.Common;
namespace OnLineExam.Web.mag.newPage
{
    /// <summary>
    /// scoreHandler 的摘要说明
    /// </summary>
    public class scoreHandler : IHttpHandler
    {
		private HttpContext _httpContext;
		private HttpRequest _httpRequest;
		private HttpResponse  _httpResponse;

        private readonly Data.OnlineExamEntities _db;
        private readonly OnLineExam.Data.EfRepository<OnLineExam.Data.Score> _scoreRepository;
        private readonly OnLineExam.Data.EfRepository<OnLineExam.Data.UserAnswer>  _userAnswerRepository;
        private readonly OnLineExam.Data.EfRepository<OnLineExam.Data.UserTestInfo> _userTestInfoRepository;
        public scoreHandler() {
            _db = new Data.OnlineExamEntities();
            _scoreRepository = new Data.EfRepository<Data.Score>(_db);
            _userAnswerRepository = new Data.EfRepository<Data.UserAnswer>(_db);
            _userTestInfoRepository = new Data.EfRepository<Data.UserTestInfo>(_db);
        }

		
		public void ProcessRequest(HttpContext context)
        {
			_httpContext = context;
			_httpRequest = context.Request;
			_httpResponse = context.Response;
			

			context.Response.ContentType = "application/json";
            

           
			var type = context.Request.Params["type"];

			switch (type)
			{
				case "getlist":
					GetList();
					break;
                case "down":
                    Down();
                    break;
                case "del":
					Delte();
					break;
				default:
					break;
			}

		}
		private void Down() {
			var id = _httpRequest.Params["id"].GetInt();// 试卷id
			var datas = _scoreRepository.TableNoTracking;
			if (id > 0)
				datas = datas.Where(s => s.PaperID == id);
			var re = datas.Select(s=>new { 人员=s.Users.UserName, 试卷=s.Paper.PaperName,单选题=s.SingleProblemMark,多选题=s.MultiProblemMark,判断题=s.JudgeProblemMark,填空题=s.FillBlankProblemMark,问答题=s.QuestionProblemMark,得分=s.UserScore,考试时间=s.ExamTime});
			WriteJson(re);

		}
        private void Delte() {
            var scoreId = _httpRequest.Params["scoreId"].GetInt();
            var score = _scoreRepository.GetById(scoreId);
            _userAnswerRepository.Delete(_userAnswerRepository.Table.Where(s => s.ScoreID == scoreId && s.UserID==score.UserID));

			if(_userTestInfoRepository.Table.Any(s => s.PaperID == score.PaperID && s.UserID == score.UserID))
			_userTestInfoRepository.Delete(_userTestInfoRepository.Table.Where(s=>s.PaperID==score.PaperID && s.UserID==score.UserID).First());

			_scoreRepository.Delete(score);

			WriteJson( Models.ServiceResult.CreateResult(0,"删除完成","") );

        }

		private void GetList() {
			var id = _httpRequest.Params["id"].GetInt();// 试卷id
			int page = _httpRequest.Params["page"].GetInt();
			int rows = _httpRequest.Params["rows"].GetInt();
            var userId = _httpRequest.Params["userid"];


            using (var ctx = new OnLineExam.Data.OnlineExamEntities())
			{
				var query = ctx.Score.AsNoTracking().AsQueryable();
				if (id > 0)
				{
					query = query.Where(s => s.PaperID == id);
				}
                if(userId.HasValue())
                    query = query.Where(s => s.UserID == userId);
                var result=EasyuiDataGrid.Create(query.Count(), query.OrderBy(s => s.ID).Skip((page - 1) * rows).Take(rows)
					.Select(s=>new { s.ID,s.UserID,  s.Paper.PaperName, s.SingleProblemMark,s.MultiProblemMark,s.JudgeProblemMark,s.FillBlankProblemMark,s.QuestionProblemMark, s.UserScore,s.ExamTime })
					.ToList());

				WriteJson(result);

			}
		}
		 
		private void WriteJson(object data) {
			_httpResponse.Write(data.SerializeObject());
			_httpResponse.End();
		}

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}